const express = require("express");
const router = express.Router();
const db = require("../utils/db");

// 渲染
router.post("/roleRender", (req, res) => {
  const { search } = req.body;
  const sql = `select * from roleManagement where roleName like '%${search}%'`;
  db.query(sql, (err, data) => {
    if (err) throw err;
    res.send({ data: data, code: 200 });
  });
});
// 删除
router.delete("/roleDelete", (req, res) => {
  const { ids } = req.body;
  const sql = `delete * from roleManagement where id in (${ids})`;
  db.query(sql, (err, data) => {
    if (err) throw err;
    res.send({ code: 200, msg: "删除成功" });
  });
});

// 添加
router.post("/roleAdd", (req, res) => {
  const { roleType, roleName, creator, creationTime, status, roleDescription } =
    req.body;
  const sql = `insert into roleManagement values (null,${roleType},${roleName},${creator},${creationTime},${status},${roleDescription})`;
  db.query(sql, (err, data) => {
    if (err) throw err;
    res.send({ code: 200, msg: "添加成功" });
  });
});

// 修改
router.post("/roleEdit", (req, res) => {
  const { id,roleType, roleName, status, roleDescription } =
    req.body;
  const sql = `update roleManagement set roleType=${roleType},roleName=${roleName},status=${status},roleDescription=${roleDescription} where id=${id};`;
  db.query(sql, (err, data) => {
    if (err) throw err;
    res.send({ code: 200, msg: "修改成功" });
  });
});

module.exports = router;
